package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.List;


@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 添加套餐
     * @param checkgroupIds
     * @param setmeal
     */
    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        //添加套餐
        setmealMapper.add(setmeal);
        //添加中间表数据(套餐和检查组)
        setSetmealAndCheckGroup(checkgroupIds,setmeal.getId());
        //判断是否上传的是照片,若果不是,则保存一个null到Redis中,会报错
       if(setmeal.getImg()!=null){
            //将保存到数据库的图片 保存在Redis中的set中
           jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            }
    }
    private void setSetmealAndCheckGroup(Integer[] checkgroupIds, Integer id) {
        if(checkgroupIds !=null &&checkgroupIds.length>0){
            setmealMapper.setSetmealAndCheckGroup(checkgroupIds,id);
        }
    }
    /**
     *
     * 查询套餐分页数据
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //定义当前页码和页面大小
        PageHelper.startPage(currentPage,pageSize);
        //条件查询
        List<Setmeal> setmealList = setmealMapper.findByCondition(queryString);
        //将查询出来的数据通过PageInfo进行封装
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查询所有套餐
     * @return
     */
    @Override
    public List<Setmeal> findAll() {
        return setmealMapper.findAll();
    }

    @Override
    public Setmeal findById(Integer id) {
        //通过套餐id查询套餐详情
        Setmeal setmeal = setmealMapper.findById(id);
        //通过套餐id查询检查组的相关信息
        List<CheckGroup> checkGroups = checkGroupMapper.findBySetmealId(setmeal.getId());

        //将查询到的检查组信息封装到套餐中
        setmeal.setCheckGroups(checkGroups);
        //遍历每一个检查组
        for (CheckGroup checkGroup : checkGroups) {
            //通过检查组的id查询检查项的集合
            List<CheckItem> checkItems = checkItemMapper.findByCheckGroupId(checkGroup.getId());
            //将所有的检查组数据封装到每一个检查组中
            checkGroup.setCheckItems(checkItems);
        }
        return setmeal;
    }


}
